package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.business.interfaces.messageV1.wechat.MessageWeChatTemplateService;
import com.zmn.oms.business.interfaces.warranty.OrderWarrantyBService;
import com.zmn.oms.business.interfaces.wechat.WechatQrScanBService;
import com.zmn.oms.common.constant.MessageRuleIdConsts;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.zmn.business.interfaces.wechat.WechatQrScanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.zmn.oms.common.constant.WechatQrConststs.*;

/**
 * 微信扫码监听
 *
 * @author: LuJia
 * @version: v1.0
 * @since: 2019/11/24 15:51
 **/
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class WechatQrScanListener {

    @Autowired
    WechatQrScanBService wechatQrScanBService;
    @Autowired
    WechatQrScanService wechatQrScanService;
    @Autowired
    MessageWeChatTemplateService messageWeChatTemplateService;
    @Autowired
    OrderWarrantyBService orderWarrantyBService;

    /**
     * 扫码
     *
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_WECHAT_SCAN_QR)
    public void scanQr(String message) {

        /**
         {
         "businessType": 100,
         "businessId": "1231937812379817",
         "userId": 1237187281371,
         "openId": "1daondsadu1i2h1313isdhau",
         "shopId": 12998
         }
         */
        log.info("#oms#scanQr message : {}", message);

        JSONObject data = JSON.parseObject(message);

        Integer businessType = data.getInteger("businessType");
        String businessId = data.getString("businessId");
        Long userId = data.getLong("userId");
        Long wechatUserId = data.getLong("wechatUserId");
        String openId = data.getString("openId");
        Integer shopId = data.getInteger("shopId");
        String nickname = data.getString("wechatNickname");
        String appName = data.getString("appName");
        String unionId = data.getString("unionId");
        // 将业务id转换为orderId
        Long orderId = Long.valueOf(businessId);

        switch (businessType) {
            case QR_TYPE_MASTER_AMOUNT_PAY:
                messageWeChatTemplateService.sendDesignatedOpenIdWeChatTemplate(orderId, MessageRuleIdConsts.ZMN_MSG_RULEID_ORDER_PAY, userId, openId);
                break;
            case QR_TYPE_MASTER_DEPOSIT_AMOUNT_PAY:
                messageWeChatTemplateService.sendDesignatedOpenIdWeChatTemplate(orderId, MessageRuleIdConsts.ZMN_MSG_RULEID_DEPOSIT_PAY, userId, openId);
                break;
            case QR_TYPE_MASTER_WARRANTY:
                // 绑定
                orderWarrantyBService.takeWarranty(orderId, userId);
                messageWeChatTemplateService.sendDesignatedOpenIdWeChatTemplate(orderId, MessageRuleIdConsts.ZMN_MSG_RULEID_WARRANTY_CARD, userId, openId);
                break;
            case QR_TYPE_MASTER_VISIT:
                wechatQrScanBService.processMasterVisit(orderId, shopId, wechatUserId, openId, nickname, appName, unionId);
                break;
            case QR_TYPE_USER_ATTENTION:
                wechatQrScanBService.processUserAttention(orderId, shopId, wechatUserId, openId, nickname, appName, unionId);
                break;
            default:
                log.error("未匹配到对应业务类型");
        }
    }
}
